﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MoneyBook.Reflection;
using MoneyBook.Web.Models;
using MoneyBook.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ComponentModel;
using MoneyBook.Web.Services;

namespace MoneyBook.DialogViews {

    [EnhancePropertyAttributeSources(typeof(Item))]
    public partial class CreateNewAccountItemDialogModel : ViewModelBase {

        private ItemContext context;

        public event EventHandler<ProcessCompletedEventArgs<Item>> CreateItemCompleted;

        public CreateNewAccountItemDialogModel() {
            this.context = new ItemContext();
        }

        [ExcludeValidation]
        [ViewModelProperty]
        private Account targetAccount_ = null;

        [ViewModelProperty]
        [MapEnhancePropertyAttributeProperties("Category", "CategoryId")]
        private Category selectedCategory_ = null;

        [ViewModelProperty]
        private DateTime? purchaseDate_ = DateTime.Now;

        [ViewModelProperty]
        private string abstract_;

        [ViewModelProperty]
        private long? expense_ = 0;

        partial void PostTargetAccountChange(Account newValue, Account oldValue) {
            if (newValue != null) {
                this.SelectedCategory = newValue.Categories.FirstOrDefault();
            }
        }

        public void CreateItem() {

            this.ValidateAllProperties();

            if (this.HasErrors) { 
                return;
            }

            Item item = new Item(){
                CategoryId = this.SelectedCategory.CategoryId,
                PurchaseDate = this.PurchaseDate.Value,
                Expense = this.Expense.Value,
                Abstract = this.Abstract,
                AccountId = this.TargetAccount.AccountId
            };

            context.Items.Add(item);

            context.SubmitChanges(op => {
                context.Items.Detach(item);

                if (op.HasError) {
                    this.AddError(op.Error.Message);
                    op.MarkErrorAsHandled();
                } else {
                    GetAddedItem(item);
                }
            }, null);
        }

        private void GetAddedItem(Item item) {
            context.Load(context.GetItemQuery(item.ItemId), op => {
                if (op.HasError) {
                    this.AddError(op.Error.Message);
                    op.MarkErrorAsHandled();
                } else {
                    Item result = op.Entities.FirstOrDefault();

                    context.Items.Detach(result);
                    if (this.CreateItemCompleted != null) {
                        this.CreateItemCompleted(this, new ProcessCompletedEventArgs<Item>(result));
                    }
                }
            }, null);
        }

        protected override EnhancePropertyAttributeType GetEnhancePtopertyAttributeType() {
            return new EnhancePropertyAttributeType(typeof(CreateNewAccountItemDialogModel));
        }
    }
}
